package com.ddstation.onsitelaw.service;


import com.ddstation.onsitelaw.entity.MaInvsBaseDrugEntity;
import com.ddstation.onsitelaw.entity.MaInvsDrugDetailEntity;
import com.ddstation.onsitelaw.mapper.MaInvsBaseDrugMapper;
import com.ddstation.onsitelaw.mapper.MaInvsDrugDetailMapper;
import com.ddstation.onsitelaw.model.MaInvsBaseDrugModel;
import com.ddstation.onsitelaw.model.MaInvsDrugDetailModel;
import com.ddstation.onsitelaw.utils.CopyBeanUtil;
import com.ddstation.onsitelaw.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.inject.Inject;
import java.util.List;

@Service
public class MaInvsBaseDrugServiceImpl implements MaInvsBaseDrugService {
    @Inject
    private MaInvsBaseDrugMapper maInvsBaseDrugMapper;
    @Inject
    private MaInvsDrugDetailMapper maInvsDrugDetailMapper;

    @Override
    @Transactional(readOnly = true)
    public PageResult queryDrugByName(String drugName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MaInvsBaseDrugEntity> maInvsBaseDrugEntityList = maInvsBaseDrugMapper.selectDrugsByName(drugName);
        PageInfo<MaInvsBaseDrugEntity> info = new PageInfo<>(maInvsBaseDrugEntityList);
        //把EntityList数据复制给ModelList
        List<MaInvsBaseDrugModel> maInvsBaseDrugModelList = CopyBeanUtil.copyObjectList(maInvsBaseDrugEntityList, MaInvsBaseDrugModel.class);
        return new PageResult(info, maInvsBaseDrugModelList);
    }

    @Override
    @Transactional(readOnly = true)
    public MaInvsBaseDrugModel showDrugDetail(Integer drugId) {
        //主键查询
        MaInvsBaseDrugEntity maInvsBaseDrugEntity = maInvsBaseDrugMapper.selectByPrimaryKey(drugId);
        //把Entity数据复制给Model
        MaInvsBaseDrugModel maInvsBaseDrugModel = CopyBeanUtil.copyObject(maInvsBaseDrugEntity, MaInvsBaseDrugModel.class);
        return maInvsBaseDrugModel;
    }


    @Override
    @Transactional(readOnly = true)
    public MaInvsDrugDetailModel getMaInvsDrugDetail(String barCode) {
        Example example = new Example(MaInvsDrugDetailEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("barCode", barCode);
        MaInvsDrugDetailEntity maInvsDrugDetailEntity = maInvsDrugDetailMapper.selectOneByExample(example);
        MaInvsDrugDetailModel maInvsDrugDetailModel = CopyBeanUtil.copyObject(maInvsDrugDetailEntity, MaInvsDrugDetailModel.class);
        return maInvsDrugDetailModel;
    }
}